/*****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
 * under the License.
 ****************************************************************/
package reco.db;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * Constraints to apply on {@link Domain}'s {@link Field}s.
 * <p>
 * They validates fields format, limits, uniqueness, nullity... whatever!.</br>
 */
public abstract class Constraint<T> {

    private final String name;
    private final String errorMessage;

    /**
     * Define a constraint with a name, over a field, with a errorMessage.
     * 
     * @param constraintName
     *            name of the constructed constraint.
     * @param constraintErrorMessage
     *            constraint errorMessage specification.
     * @throws NullPointerException
     *             if <code>constraintName</code> is <code>null</code>.
     * @throws NullPointerException
     *             if <code>constraintField</code> is <code>null</code>.
     * @throws NullPointerException
     *             if <code>constraintMessageSpec</code> is <code>null</code>.
     * @throws IllgalArgumentException
     *             if <code>constraintMessageSpec</code> is a blank string.
     */
    protected Constraint(String constraintName, String constraintErrorMessage) {
        this.name = checkNotNull(constraintName);

        checkNotNull(constraintErrorMessage, "constraintErrorMessage");
        checkArgument(!isBlank(constraintErrorMessage), "constraintErrorMessage is blank");
        this.errorMessage = constraintErrorMessage.trim();
    }

    /**
     * Gets the constraint name.
     * 
     * @return a not <code>null</code> name.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Gets the errorMessage to be presented when the contraint fails.
     * 
     * @return a not <code>null</code> errorMessage.
     */
    public String getErrorMessage() {
        return this.errorMessage;
    }

    /**
     * Validates a value.
     * 
     * @param value
     *            Value to be validated under the constraint
     * @return <code>true</code> if the values is valid; <code>false</code> in other way.
     */
    public abstract boolean validate(T value);

    public boolean validateValue(Object fieldValue) {
        try {
            @SuppressWarnings("unchecked")
            final T value = (T) fieldValue;
            return validate(value);
        } catch (final ClassCastException e) {
            return false;
        }
    }

    public abstract Object[] getErrorArguments();
}
